package cn.stylefeng.guns.modular.business.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.business.entity.MoneyLog;
import cn.stylefeng.guns.modular.business.mapper.MoneyLogMapper;
import cn.stylefeng.guns.modular.business.model.dto.AmountQueryCondition;
import cn.stylefeng.guns.modular.business.model.params.MoneyLogParam;
import cn.stylefeng.guns.modular.business.model.result.MoneyLogResult;
import cn.stylefeng.guns.modular.business.service.MoneyLogService;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 金额变动表 服务实现类
 * </p>
 *
 * @author jwj
 * @since 2019-07-16
 */
@Service
public class MoneyLogServiceImpl extends ServiceImpl<MoneyLogMapper, MoneyLog> implements MoneyLogService {

    @Override
    public void add(MoneyLogParam param) {
        MoneyLog entity = getEntity(param);
        this.save(entity);
    }

    @Override
    public void delete(MoneyLogParam param) {
        this.removeById(getKey(param));
    }

    @Override
    public void update(MoneyLogParam param) {
        MoneyLog oldEntity = getOldEntity(param);
        MoneyLog newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public MoneyLogResult findBySpec(MoneyLogParam param) {
        return null;
    }

    @Override
    public List<MoneyLogResult> findListBySpec(MoneyLogParam param) {
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(MoneyLogParam param) {
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(MoneyLogParam param) {
        return param.getMoneyId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private MoneyLog getOldEntity(MoneyLogParam param) {
        return this.getById(getKey(param));
    }

    private MoneyLog getEntity(MoneyLogParam param) {
        MoneyLog entity = new MoneyLog();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

    /**
     * 统计用户余额
     *
     * @param userId 用户id
     * @return
     */
    @Override
    public BigDecimal totalAmount(Long userId) {
        return this.baseMapper.totalAmount(userId);
    }

    @Override
    public List<MoneyLogResult> record(Page page, Long userId, AmountQueryCondition amountQueryCondition) {
        return baseMapper.record(page, userId, amountQueryCondition);
    }
}
